//
//  DrawGrid.m
//  Ikinloop-demo
//
//  Created by Li Shen on 2021/4/22.
//  Copyright © 2021 lyh. All rights reserved.
//

#import "DrawGrid.h"
#import "DrawUtils.h"

@interface DrawGrid()

@property (nonatomic, strong) CALayer* layer;
@property (nonatomic, assign) CGFloat startX;
@property (nonatomic, assign) CGFloat startY;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat heigth;
@property (nonatomic, assign) CGFloat mmPixels;

@property (nonatomic, strong) DrawUtils *drawUtils;
@property (nonatomic, assign) CGContextRef context;

@end
@implementation DrawGrid

- (instancetype)initWithLayer:(CALayer*)layer{
    if(self = [super init]){
        self.layer = layer;
        [self setupDefaultValues];
    }
    return self;
}

- (instancetype)initWithLayer:(CALayer*)layer context:(CGContextRef)context{
    self = [super init];
    if (self) {
        self.layer = layer;
        self.context = context;
        [self setupDefaultValues];
    }
    return self;
}

- (void)setupDefaultValues{
    self.drawUtils = [[DrawUtils alloc] initWithLayer:self.layer context:self.context];
    self.smallgridColor = [UIColor greenColor];
    self.bgColor = [UIColor blackColor];
    self.gridColor = [UIColor greenColor];
    self.mmPixels = [DrawUtils mm2pt:1.0];
    
    self.heigth = self.layer.frame.size.height;
    self.width = self.layer.frame.size.width;
    self.startX = 0;
    self.startY = 0;
    
    self.gridLineWidth = 0.2;
    self.smallGridLineWidth = 0.1;
}

- (void)reload{
    self.heigth = self.layer.frame.size.height;
    self.width = self.layer.frame.size.width;
}

- (void)drawBackgroundGridWithContext{
    int rows = (int)floor(self.heigth / _mmPixels);
    int cols = (int)floor(self.width / _mmPixels);
    
    CGFloat maxw = floor(cols * _mmPixels);
    CGFloat maxh = floor(rows * _mmPixels);
    [self.drawUtils fillRect:CGRectMake(0, 0, self.width, self.heigth) color:self.bgColor];
    //画横向
    for (int i = 0; i <= rows; i++) {
        CGFloat stepY = round(_mmPixels * i);
        if(i % 5 == 0){
            [self.drawUtils beginPath:self.gridColor lineWidth:self.gridLineWidth lineCap:kCGLineCapButt lineJoin:kCGLineJoinMiter];
            [self.drawUtils drawLine:CGPointMake(_startX, _startY+stepY)  end:CGPointMake(_startX + maxw, _startY + stepY)];
            [self.drawUtils endPath];
        }else{
            [self.drawUtils beginPath:self.smallgridColor lineWidth:self.smallGridLineWidth lineCap:kCGLineCapButt lineJoin:kCGLineJoinMiter];
            [self.drawUtils drawLine:CGPointMake(_startX, _startY+stepY)  end:CGPointMake(_startX + maxw, _startY + stepY)];
            [self.drawUtils endPath];
        }
    }
    
    //画横向
    for (int j = 0; j <= cols; j++) {
        CGFloat stepX = round(_mmPixels * j);
        if(j % 5 == 0){
            [self.drawUtils beginPath:self.gridColor lineWidth:self.gridLineWidth lineCap:kCGLineCapButt lineJoin:kCGLineJoinMiter];
            [self.drawUtils drawLine:CGPointMake(_startX+stepX, _startY)  end:CGPointMake(_startX + stepX, _startY + maxh)];
            [self.drawUtils endPath];
        }else{
            [self.drawUtils beginPath:self.smallgridColor lineWidth:self.smallGridLineWidth lineCap:kCGLineCapButt lineJoin:kCGLineJoinMiter];
            [self.drawUtils drawLine:CGPointMake(_startX+stepX, _startY)  end:CGPointMake(_startX + stepX, _startY + maxh)];
            [self.drawUtils endPath];
        }
    }
}


- (void)drawBackgroundGrid{
    self.layer.backgroundColor = self.bgColor.CGColor;
    [self.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
    
    self.heigth = self.layer.frame.size.height;
    self.width = self.layer.frame.size.width;
    int rows = (int)floor(self.heigth / _mmPixels);
    int cols = (int)floor(self.width / _mmPixels);
    
    CGFloat maxw = floor(cols * _mmPixels);
    CGFloat maxh = floor(rows * _mmPixels);
    
    //画横向
    for (int i = 0; i <= rows; i++) {
        CGFloat stepY = round(_mmPixels * i);
        if(i % 5 == 0){
            self.drawUtils.drawerColor = self.gridColor;
            [self.drawUtils drawLineStartPoint:CGPointMake(_startX, _startY+stepY) toEndPoint:CGPointMake(_startX + maxw, _startY + stepY) withLineWidth:_gridLineWidth];
        }else{
            self.drawUtils.drawerColor = self.smallgridColor;
            [self.drawUtils drawLineStartPoint:CGPointMake(_startX, _startY+stepY) toEndPoint:CGPointMake(_startX + maxw, _startY + stepY) withLineWidth:_smallGridLineWidth];
        }
    }
    
    //画横向
    for (int j = 0; j <= cols; j++) {
        CGFloat stepX = round(_mmPixels * j);
        if(j % 5 == 0){
            self.drawUtils.drawerColor = self.gridColor;
            [self.drawUtils drawLineStartPoint:CGPointMake(_startX+stepX, _startY) toEndPoint:CGPointMake(_startX + stepX, _startY + maxh) withLineWidth:_gridLineWidth];
        }else{
            self.drawUtils.drawerColor = self.smallgridColor;
            [self.drawUtils drawLineStartPoint:CGPointMake(_startX+stepX, _startY) toEndPoint:CGPointMake(_startX + stepX, _startY + maxh) withLineWidth:_smallGridLineWidth];
        }
    }
}

@end
